package com.lfx.sort;

/**
 * @author 李逢喜
 * create 2021/7/16
 */
public class BubbleSort {

    public static void main(String[] args) {
        long start=System.currentTimeMillis();
        method1();
        long end=System.currentTimeMillis();
        System.out.println("[耗时:"+(end-start)+"]");


        long start2=System.currentTimeMillis();
        method2();
        long end2=System.currentTimeMillis();
        System.out.println("[耗时:"+(end2-start2)+"]");
    }

    public static void method1(){
        int[] nums={4,1,6,3,7,2,9,5,50,20};
        for (int i = 0; i < nums.length-1; i++) {
            for (int j = 0; j < nums.length-1-i; j++) {
                int tmp=0;
                if(nums[j]>nums[j+1]){
                    tmp=nums[j];
                    nums[j]=nums[j+1];
                    nums[j+1]=tmp;
                }
            }
        }

        for (int num : nums) {
            System.out.println(num);
        }
    }


    public static void method2(){
        int[] nums={4,1,6,3,7,2,9,5,50,20};
        for (int i = 0; i < nums.length-1; i++) {
            boolean isSort=true;
            for (int j = 0; j < nums.length-1-i; j++) {
                int tmp=0;
                if(nums[j]>nums[j+1]){
                    isSort=false;
                    tmp=nums[j];
                    nums[j]=nums[j+1];
                    nums[j+1]=tmp;
                }
            }
            if (isSort)
                break;
        }

        for (int num : nums) {
            System.out.println(num);
        }
    }
}
